package com.etc.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.etc.dao.OrdersMapper;
import com.etc.entity.Orders;
import com.etc.service.OrdersService;
import com.etc.tools.PageData;

/**
 * 寄养订单service的实现层
 * 
 * @author 薛佳鑫
 *
 */
@Service(value = "ordersService")
public class OrdersImpl implements OrdersService {
	@Resource
	private OrdersMapper om;
	Logger logger=Logger.getLogger(OrdersImpl.class);

	public PageData<Orders> getOrdersByPage(int page, int pageSize, String content) {
		// TODO Auto-generated method stub
		int start = (page - 1) * pageSize;
		List list = om.selectOrdersByPage(start, pageSize, content);
		int count = om.selectCount(content);
		PageData<Orders> pd = new PageData<>(list, count, pageSize, page, 0, "操作成功");
		return pd;
	}

	@Override
	public Orders getOrderById(int oid) {
		// TODO Auto-generated method stub
		return om.selectByPrimaryKey(oid);
	}

	@Override
	public boolean addOrder(Orders orders) {
		// TODO Auto-generated method stub
		if (orders != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar cal = Calendar.getInstance();
			try {
				cal.setTime(sdf.parse(orders.getOstdate()));
				long time1 = cal.getTimeInMillis();
				cal.setTime(sdf.parse(orders.getOenddate()));
				long time2 = cal.getTimeInMillis();
				long day = (time2 - time1) / (1000 * 3600 * 24);
				int days=Integer.parseInt(String.valueOf(day));
				//不足一天就设置天数为1
				if(days==0) {
					orders.setOdays(1);
				}else {
					orders.setOdays(days);
				}
				orders.setOdeposit(orders.getOdays()*100/2);
				orders.setOfinapay(orders.getOdays()*100/2);
				orders.setOtotal(orders.getOdays()*100);
				return om.insert(orders) > 0;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return false;
	}

	@Override
	public boolean delOrderById(int oid) {
		// TODO Auto-generated method stub
		return om.deleteByPrimaryKey(oid) > 0;
	}

	@Override
	public boolean updateOrderById(Orders orders) {
		// TODO Auto-generated method stub
		if (orders != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar cal = Calendar.getInstance();
			try {
				cal.setTime(sdf.parse(orders.getOstdate()));
				long time1 = cal.getTimeInMillis();
				cal.setTime(sdf.parse(orders.getOenddate()));
				long time2 = cal.getTimeInMillis();
				long day = (time2 - time1) / (1000 * 3600 * 24);
				int days=Integer.parseInt(String.valueOf(day));
				if(days==0) {
					orders.setOdays(1);
				}else {
					orders.setOdays(days);
				}
				orders.setOdays(days);
				orders.setOdeposit(orders.getOdays()*100/2);
				orders.setOfinapay(orders.getOdays()*100/2);
				orders.setOtotal(orders.getOdays()*100);
				return om.updateByPrimaryKeySelective(orders) > 0;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return false;
	}

	@Override
	public List<Orders> getOrdersByUid(int ouid) {
		// TODO Auto-generated method stub
		return om.selectOrdersByUid(ouid);
	}

	@Override
	public boolean updateStatus(int oid) {
		// TODO Auto-generated method stub
		return om.updateStatus(oid) > 0;
	}


	@Override
	public List<Orders> getOrdersByUname(String otuname) {
		
		return om.selectOrdersByUname(otuname);
	}

}
